package web.java.daoImpl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import web.java.bean.Employee;
import web.java.dao.EmployeeDao;

public class EmployeeDaoImpl implements EmployeeDao{
	private BaseDaoImpl bdi=new BaseDaoImpl();
	@Override
	public <T> List<T> select(Object[] params, Class<T> clazz) {
		return bdi.select(params, clazz);
	}

	@Override
	public List<Employee> getAll() {
		return bdi.select(null,Employee.class);
	}

	@Override
	public List<Employee> getPage(int pages, int rows) {
		List<Employee> list=new ArrayList<Employee>();
		QueryRunner runner=new QueryRunner();
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		Connection conn=null;
		String sql="select * from emp limit ?,?";
		try {
			conn=dataSource.getConnection();
			list=runner.query(conn,sql,new BeanListHandler<Employee>(Employee.class),pages,rows);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public int getCount() {
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		QueryRunner runner=new QueryRunner();
		Connection conn=null;
		long sum;
		try {
			conn=dataSource.getConnection();
			String sql="SELECT COUNT(emp_id) FROM emp;";
			sum=runner.query(conn,sql,new ScalarHandler<Long>());
			conn.close();
			return (int)sum;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	//测试
	public static void main(String[] args) {
		EmployeeDaoImpl edi=new EmployeeDaoImpl();
		/*List<Employee> list=edi.getPage(1,5);
		for(Employee emp:list){
			System.out.println(emp);
		}*/
		System.out.println(edi.getCount());
	}
}
